package com.example.healkep.View.foodShow;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import com.example.healkep.FoodBaseAdapterActivity;
import com.example.healkep.FoodNewsDetailActivity;
import com.example.healkep.LoginActivity;
import com.example.healkep.R;
import com.example.healkep.adapter.FoodListAdapter;
import com.example.healkep.bean.Food;
import com.example.healkep.bean.ItemBean;
import com.example.healkep.component.page.PageView;
import com.example.healkep.listener.HttpListener;
import com.example.healkep.utils.HttpUtil;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.security.KeyException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodShowActivity extends AppCompatActivity implements FoodListAdapter.OnItemClickListener {
    private PageView pageView;
    private FoodListAdapter adapter;
    private String keyword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_show);

        keyword = (String) getIntent().getStringExtra("searchname");
        if(keyword == null)
            keyword = "";
        initComponent();
    }
    private void initComponent(){
        pageView = findViewById(R.id.pageView);
        adapter = new FoodListAdapter(new ArrayList<>(),this,this);
        pageView.setAdapter(adapter);
        pageView.setNextPageListener((Index, adapter1) -> {
            LoadData(Index+1,keyword);
        });
        pageView.setLastPageListener((Index, adapter1) -> {
            LoadData(Index-1,keyword);
        });
        LoadData(1,keyword);
    }

    @Override
    public void ClickListener(Food food) {
        ItemBean itemBean = new ItemBean();
        itemBean.setContent(food.getIngredients());
        itemBean.setTitle(food.getName());
        itemBean.setImgResId(R.drawable.test1);
        Intent intent = new Intent(FoodShowActivity.this, FoodNewsDetailActivity.class);
        intent.putExtra("item",itemBean);
        intent.putExtra("id",food.getId());
        intent.putExtra("name",food.getName());
        intent.putExtra("ingredients",food.getIngredients());
        intent.putExtra("accessories",food.getAccessories());
        intent.putExtra("introduction",food.getIntroduction());
        intent.putExtra("label",food.getLabel());
        intent.putExtra("cooking",food.getCooking());
        intent.putExtra("favor",food.getFavor());
        intent.putExtra("functions",food.getFunctions());
        intent.putExtra("target",food.getTarget());
        intent.putExtra("disease",food.getDisease());
        intent.putExtra("constitution",food.getConstitution());
        intent.putExtra("cuisine",food.getCuisine());
        intent.putExtra("snack",food.getSnack());
        intent.putExtra("type",food.getType());
        intent.putExtra("kitchenware",food.getKitchenware());
        intent.putExtra("occasion",food.getOccasion());
        startActivity(intent);
    }
    public void LoadData(int index,String keywords){
        HttpUtil<List<Food>> helper = new HttpUtil<>();
        Map<String,Object> map = new HashMap<>();
        map.put("keyword",keywords);
        map.put("pageIndex",index);
        helper.post(LoginActivity.url + "/food/get", helper.getRequestBody(map), new HttpListener<List<Food>>() {
            @Override
            public void success(List<Food> data) {
                adapter.update(data);
                if(data.size()<20){
                    pageView.setLastPage(true);
                }else{
                    pageView.setLastPage(false);
                }
                pageView.pageFinished();
            }
            @Override
            public void failed(int code, String message, String detailMessage) {
                Toast.makeText(FoodShowActivity.this,message,Toast.LENGTH_SHORT).show();
            }
        },new TypeToken<List<Food>>(){}.getType(),this);
    }
}